package com.visionet.school.app.web.wechat.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.visionet.school.app.web.entity.wechat.Menu;
import com.visionet.school.app.web.service.wechat.MenuService;
import com.visionet.school.app.web.wechat.vo.ButtonVO;
import com.visionet.school.app.web.wechat.vo.MainVO;
import com.visionet.school.app.web.wechat.vo.MenuButtonVO;
import com.visionet.school.app.web.wechat.vo.MenusVO;

import java.util.ArrayList;
import java.util.List;

/*
 * 微信菜单接口操作类
 */

public class MenuUtil {
	
	/*
	 * 创建菜单
	 */
	public static String CreateMenus(String params)
	{
		String strURL= MainVO.GetMenuUrl("create");
		System.out.println(params);
		return DataTransfer.PostDataToWeiXin(strURL, params);
	}
	/*
	 * 删除菜单
	 */
	public static String DeleteMenus()
	{
		String strURL=MainVO.GetMenuUrl("delete");
		return DataTransfer.PostDataToWeiXin(strURL, null);
	}
	/*
	 * 获取全部菜单
	 */
	public static String GetMenuJson()
	{
		String strURL=MainVO.GetMenuUrl("get");
		return DataTransfer.PostDataToWeiXin(strURL, null);
	}
	public static MenusVO GetMenuObject()
	{
		String strURL=MainVO.GetMenuUrl("get");
		String datasString=DataTransfer.PostDataToWeiXin(strURL, null);
		MenusVO menusVO=JSONObject.parseObject(datasString,MenusVO.class);
		return menusVO;
	}
	/*
	 * 微信菜单创建
	 */
	public static String SubmitMenus(MenuService menuService)
	{
		List<Menu> lstParentCmWxmenus = menuService.findShowParent();
		
		//StringBuilder sBuilder=new StringBuilder("{");
		ButtonVO buttonVO=new ButtonVO();
		List<MenuButtonVO> menus=new ArrayList<>();
		for (Menu menu : lstParentCmWxmenus) {
			List<Menu> lstChildCmWxmenus=menuService.findChildMenus(menu.getId());
			MenuButtonVO menuButtonVO=new MenuButtonVO();			
			menuButtonVO.setName(menu.getMenuName());
			if(lstChildCmWxmenus.size()>0)
			{
				List<MenuButtonVO> menuchilds=new ArrayList<MenuButtonVO>();
				for(Menu childMenu :lstChildCmWxmenus)
				{					
					MenuButtonVO menuButtonVO2=new MenuButtonVO();
					menuButtonVO2.setName(childMenu.getMenuName());
					switch (childMenu.getMenuType()){
						case "0":
							menuButtonVO2.setUrl(childMenu.getMenuUrl());
							break;
						case "1":
							menuButtonVO2.setKey(childMenu.getId());
							break;
					}
					menuButtonVO2.setType(childMenu.getMenuType());
					menuchilds.add(menuButtonVO2);
				}
				menuButtonVO.setSub_button(menuchilds);
				menus.add(menuButtonVO);
			}else {
				switch (menu.getMenuType()){
					case "0":
						menuButtonVO.setUrl(menu.getMenuUrl());
						break;
					case "1":
						menuButtonVO.setKey(menu.getId());
						break;
				}
				menuButtonVO.setType(menu.getMenuType());
				menus.add(menuButtonVO);
			}
		}
		buttonVO.setButton(menus);
//        SpringContextUtil.getBean(WechatService.class).initToken();
		return CreateMenus(JSON.toJSONString(buttonVO));
	}

    /**
     * 定制个性化菜单
     * @param jsonStr
     * @return
     */
    public static String individuationMenu(String jsonStr){
        String strURL= MainVO.individuationMenu();
        return DataTransfer.PostDataToWeiXin(strURL, jsonStr);
    }

}
